package husc.se.ndung.soiphubaimobile;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;

import java.util.ArrayList;


public class Login extends ActionBarActivity {
    public static String UserName;
    public static String Password;
    private ProgressDialog m_ProgressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        m_ProgressDialog = new ProgressDialog(this);
        m_ProgressDialog.setTitle(getString(R.string.processLogin));
        m_ProgressDialog.setMessage(getResources().getText(R.string.waitingLabel));
        // m_ProgressDialog.setProgressStyle(m_ProgressDialog.STYLE_HORIZONTAL);
        m_ProgressDialog.setMax(20);
        m_ProgressDialog.setProgress(0);
    }

    public void dangNhap(View v) {
        try {
            EditText txtUserName = (EditText) findViewById(R.id.txtUserName);
            EditText txtPassword = (EditText) findViewById(R.id.txtPassword);
            UserName = txtUserName.getText().toString();
            Password = txtPassword.getText().toString();
            aysnLogin();
        } catch (Exception ex) {
            Log.d("Loi", ex.toString());
        }
    }

    private String logIn() throws Exception {
        ArrayList<String> params = new ArrayList<String>();
        params.add("userName:" + UserName);
        params.add("passWord:" + Password);
        SoapPrimitive response = (SoapPrimitive) Utils.sendRequest(this, "LogIn", params);
        return response.toString();
    }

    public void aysnLogin() {

        final AsyncTask<Void, Integer, Void> asyncTaskLoad = new AsyncTask<Void, Integer, Void>() {

            String msg;

            @Override
            protected Void doInBackground(Void... params) {
                try {
                    msg = logIn();
                } catch (Exception e) {
                    Log.d("Loi", e.toString());
                    msg = "error";
                } finally {
                    return null;
                }
            }

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                m_ProgressDialog.show();
            }

            @Override
            protected void onPostExecute(Void aVoid) {
                super.onPostExecute(aVoid);
                AlertDialog.Builder builder = new AlertDialog.Builder(Login.this);
                m_ProgressDialog.dismiss();
                if (msg != null) {
                    if (msg.compareTo("error") == 0) {
                        builder.setTitle(getString(R.string.errorTitle));
                        builder.setMessage(getString(R.string.errorMessage));
                    } else {
                        if (msg.compareTo("true") == 0) {
                            Utils.setKeyValue(Login.this,"UserName", UserName);
                            Utils.setKeyValue(Login.this,"Password", Password);
                            finish();
                            Intent intent = new Intent(Login.this,MainActivity.class);
                            startActivity(intent);
                        } else {
                            builder.setTitle(getString(R.string.loginfail));
                            builder.setMessage(getString(R.string.reasonLoginFail));
                        }
                    }
                    builder.setNegativeButton(R.string.okbutton, null);
                    builder.show();
                }
            }

            @Override
            protected void onProgressUpdate(Integer... values) {
                super.onProgressUpdate(values);
                Log.d("Asyntask values: ", values.toString());
            }
        }.execute();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_login, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            Intent intent = new Intent(Login.this, SettingActivity.class);
            //startActivity(intent);
            startActivityForResult(intent, 1);

            return true;
        }

        return super.onOptionsItemSelected(item);
    }
    public  void config(View v){
        Intent intent = new Intent(Login.this, SettingActivity.class);
        startActivityForResult(intent, 1);
    }
}
